package com.example.controleautomovel.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.controleautomovel.R;
import com.example.controleautomovel.adapter.AdaptadorListaAutomovel;
import com.example.controleautomovel.adapter.AutomovelDBAdapter;
import com.example.controleautomovel.pojo.Automovel;

public class ListaAutomovelActivity extends Activity implements OnItemClickListener, OnItemLongClickListener{

	private static final int REQUISICAO_GALERIA = 200;
	
	ListView listAutomoveis;
	ImageView imgFotoGaleria = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lista_automovel);
		
		listAutomoveis = (ListView) findViewById(R.id.listAutomoveis);
		
		AutomovelDBAdapter db = new AutomovelDBAdapter(getBaseContext());
		db.open();
		ArrayList<Automovel> lista = db.listaAutomoveis();
		db.close();
		
		AdaptadorListaAutomovel adaptador = new AdaptadorListaAutomovel(getBaseContext(), lista);
		
		listAutomoveis.setAdapter(adaptador);
		listAutomoveis.setOnItemClickListener(this);
		listAutomoveis.setOnItemLongClickListener(this);
		
//		Intent it = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//		startActivityForResult(it, REQUISICAO_GALERIA);
	}

//	@Override
//	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		// TODO Auto-generated method stub
//		super.onActivityResult(requestCode, resultCode, data);
		
//		if(requestCode == REQUISICAO_GALERIA){
//			String nomeFoto = automovel.getNome()+automovel.getPlaca();
//			
//			Uri uriFoto = data.getData();
//			
//			Log.d("Path", uriFoto.toString());
//			
//			Bitmap imagemGaleria = null;
//			
//			try {
//				imagemGaleria = BitmapFactory.decodeStream(getContentResolver().openInputStream(uriFoto));
//				
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			
//			imgFotoGaleria.setImageBitmap(imagemGaleria);
//			
//		}
//	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.lista_automovel, menu);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
//		TextView txtIdContato = (TextView) arg1.findViewById(R.id.txtIdAutomovel);
//		
//		Intent it = new Intent(getBaseContext(), ActivityEditaAutomovel.class);
//		it.putExtra("ID", txtIdContato.getText().toString());
//		finish();
//		startActivity(it);
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		TextView txtIdContato = (TextView) arg1.findViewById(R.id.txtIdAutomovel);
		
		int id = Integer.parseInt(txtIdContato.getText().toString());
		
		AutomovelDBAdapter db = new AutomovelDBAdapter(getBaseContext());
		db.open();
		db.deletarAutomovel(id);
		db.close();
		
		finish();
		startActivity(getIntent());
		
		return false;
	}


}
